import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


 public class WriteInDomainIfNotExist  { 

    String filePath2 = "C:\\test3.txt";
    BufferedReader br;
    String line = "";
    String read="";
    int count=0;
    ArrayList<String> storeWordList = new ArrayList<String>();

    List<String> tem = new LinkedList<String>();
    List<String> temp = new LinkedList<String>();
    List<String> temps = new LinkedList<String>();
    List<String> tempsPredicate = new LinkedList<String>();
    String[] predicate;
    String String2 [];
    String String1[]= predicate();
    String String[]=S();

	ListOfObjects Obj=new ListOfObjects();
	
     Icon icon =new Icon();
     
ProblemWithoutMain p = new ProblemWithoutMain();
public String[] S(){
    try {
        FileInputStream fstream = new FileInputStream("test3.txt");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        //Read File Line By Line
        boolean found = false;
        while ((strLine = br.readLine()) != null) {
        	
        	String [] tokens = strLine.split("\\s+");
        	for(int i=0; i < tokens.length; i++)
        	{
                if(tokens[i].contains("predicate"))
                {
                	found = true;
                }
                if(found)
                	break;
            	storeWordList.add(tokens[i]);
        	}
        }
        //Close the input stream

        in.close();
        

    } catch (Exception e) {//Catch exception if any
        System.err.println( e.getMessage());
    }
    String s []= storeWordList.toArray(new String[0]);

    return s;
}



public String[] predicate(){
	
	
	 try {
	    	
	        br = new BufferedReader(new FileReader(filePath2));
	        try {
	        	
	            while((line = br.readLine()) != null)
	            {
	            	
	        		read=br.readLine();

	            	String[] words = line.split(" ");
	            	

	            	 for (String word : words) {

	          	 if(word.equals("(:predicates")){
	          	
	           		 while((read = br.readLine()) != null){
	            	  tempsPredicate.add(read);
	           		 
	               	 }
	           	 }
	            	 }
	           
	                }
	            
          		 predicate	 = tempsPredicate.toArray(new String[0]);

	            
	            br.close();
	           
	        } catch (IOException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	    } catch (FileNotFoundException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	    
         return predicate;
}


public void display(){
	for(int i=0 ; i< String1.length; i++){
		System.out.print(String1[i]);

	}
}

public void WriteInDomain() throws FileNotFoundException, UnsupportedEncodingException{
    ListOfTypes t =new ListOfTypes(4);

	 String Types[]=t.T();

	PrintWriter writer = new PrintWriter("WriteInDomainIfNotExist.txt", "UTF-8");
	for(int i=0 ; i<String.length; i++){
		writer.println(String[i]);
	}
	writer.print(";;(:types");
	for(int i=0 ; i< 3; i++){
		writer.print( " "+Types[i] );
	}
	writer.println("");

	writer.println("(:predicate");
	for(int i=0 ; i<String1.length ; i++){
		writer.println( String1[i] );

	}
	
	writer.close();
}
}
	

